from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.vo.attendance_vo import AttendanceModel
from module_admin.entity.do.attendance_do import AttendanceInfo
from sqlalchemy import bindparam, func, or_, select, update
from utils.page_util import PageUtil
from module_admin.entity.do.employee_do import EmployeeInfo


class AttendanceDao:
    """
    考勤管理模块数据库操作层
    """

    @classmethod
    async def get_attendance_info_for_edit_option(cls, db: AsyncSession, attendance_info: AttendanceModel, data_scope_sql: str):
        """
        获取考勤编辑对应的信息列表

        :param db: orm对象
        :param attendance_info: 考勤对象
        :param data_scope_sql: 数据权限对应的查询sql语句
        :return: 考勤列表信息
        """
        attendance_result = (
            (
                await db.execute(
                    select(AttendanceInfo)
                    .where(
                        AttendanceInfo.del_flag == '0'
                    )
                    .order_by(AttendanceInfo.id)
                )
            )
            .scalars()
            .all()
        )

        return attendance_result

    @classmethod
    async def get_attendance_list(cls, db: AsyncSession, page_object: AttendanceModel, data_scope_sql: str, is_page: bool = False):
        """
        根据查询参数获取考勤列表信息

        :param db: orm对象
        :param page_object: 不分页查询参数对象
        :param data_scope_sql: 数据权限对应的查询sql语句
        :param is_page: 是否开启分页
        :return: 考勤列表信息对象
        """
        query = (
            select(
                AttendanceInfo.id,
                AttendanceInfo.employee_id,
                EmployeeInfo.name.label('employee_name'),
                AttendanceInfo.attendance_date,
                AttendanceInfo.status,
                AttendanceInfo.hours_worked,
                AttendanceInfo.del_flag,
                AttendanceInfo.create_by,
                AttendanceInfo.create_time,
                AttendanceInfo.update_by,
                AttendanceInfo.update_time
            )
            .join(EmployeeInfo, AttendanceInfo.employee_id == EmployeeInfo.id)
            .where(
                AttendanceInfo.del_flag == '0',
                AttendanceInfo.id == page_object.id if page_object.id is not None else True,
                AttendanceInfo.employee_id == page_object.employee_id if page_object.employee_id is not None else True,
                AttendanceInfo.status == page_object.status if page_object.status is not None else True,
                AttendanceInfo.attendance_date == page_object.attendance_date if page_object.attendance_date is not None else True,
                eval(data_scope_sql),
            )
            .order_by(AttendanceInfo.id)
            .distinct()
        )
        attendance_list = await PageUtil.paginate(db, query, page_object.page_num, page_object.page_size, is_page)

        return attendance_list
    
    @classmethod
    async def add_attendance(cls, db: AsyncSession, attendance: AttendanceModel):
        """
        新增考勤信息
        :param db: orm对象
        :param attendance: 考勤信息对象
        :return: 新增考勤的主键ID或完整对象
        """
        attendance_dict = {
            'employee_id': attendance.employee_id,
            'attendance_date': attendance.attendance_date,
            'status': attendance.status,
            'hours_worked': attendance.hours_worked,
            'del_flag': attendance.del_flag,
            'create_by': attendance.create_by,
            'create_time': attendance.create_time,
            'update_by': attendance.update_by,
            'update_time': attendance.update_time,
        }
        db_attendance = AttendanceInfo(**attendance_dict)
        db.add(db_attendance)
        await db.flush()
        return db_attendance 